package com.remote;

import android.app.Activity;
import android.os.*;
import android.content.ServiceConnection;
import android.content.ComponentName;
import android.content.Intent;
import android.content.Context;
import android.util.Log;
import android.view.Window;
import android.graphics.PixelFormat;


public abstract class Remote extends Activity{
    
    protected final String TAG = this.getClass().getName();       // loging TAG as name of the Activity
    
    Messenger Pipe = null;                                  // sending data to service
    boolean isBound;                                        // boolean if Pipe is binded to service
    
    protected class IncomingHandler extends Handler{
        public void handleMessage(Message msg){
            parseMessage(msg);
        }
    }
    
    //! parses messages received by handler -- workaround
    protected void parseMessage(Message msg){};
    
    protected final Messenger msgReceiver = new Messenger(new IncomingHandler());

    protected ServiceConnection Connection = new ServiceConnection(){
        @Override
        public void onServiceConnected(ComponentName className,IBinder service){
            Log.i(TAG,"onServiceConnected");
            Pipe = new Messenger(service);
            Log.i(TAG,"Pipe created");
            
            sendMsg(Params.MSG_REGISTER_ACTIVITY);
            
            Log.i(TAG,"Service connected");
        }
        
        @Override
        public void onServiceDisconnected(ComponentName className){
            doUnbindService();
            Pipe = null;
            Log.i(TAG,"Disconnected");
        }
    };
    
    @Override
    public void onStart(){
        super.onStart();
        doBindService();
        if(Pipe != null) sendMsg(Params.MSG_REGISTER_ACTIVITY);
    }
    
    @Override
	public void onAttachedToWindow() {
		super.onAttachedToWindow();
		Window window = getWindow();
		window.setFormat(PixelFormat.RGBA_8888);
	}
    
    @Override
    public void onDestroy(){
        super.onDestroy();
        doUnbindService();
    }
    
    protected void doBindService(){
        startService(new Intent(this,Gateway.class));
        bindService(new Intent(this,Gateway.class),Connection,0);
        isBound = true;
        Log.i(TAG,"Binding");
    }
    
    protected void doUnbindService(){
        if(isBound){
/*
            if(Pipe != null){
                try{
                    Message msg = Message.obtain(null,Params.MSG_UNREGISTER_ACTIVITY);
                    msg.replyTo = msgReceiver;
                    Pipe.send(msg);
                }catch(RemoteException e){
                    Log.i(TAG,"Service Crashed");
                }
            }
*/
            
            unbindService(Connection);
            Pipe = null;
            isBound = false;
            Log.i(TAG,"Unbinding");
        }
    }
    
    protected boolean sendMsg(int what){
        try{
            Message msg = Message.obtain(null,what);
            msg.replyTo = this.msgReceiver;
            Pipe.send(msg);
        }catch(RemoteException e){
            Log.e(TAG,"Service not running");
            return false;
        }
        return true;
    }
    
    protected boolean sendMsg(int what,int arg1){
        try{
            Message msg = Message.obtain(null,what);
            msg.replyTo = this.msgReceiver;
            msg.arg1=arg1;
            Pipe.send(msg);
        }catch(RemoteException e){
            Log.e(TAG,"Service not running");
            return false;
        }
        return true;
    }
    
    protected boolean sendMsg(int what,Bundle bData){
        try{
            Message msg = Message.obtain(null,what);
            msg.replyTo = this.msgReceiver;
            if(bData!=null) msg.setData(bData);
            Pipe.send(msg);
        }catch(RemoteException e){
            Log.e(TAG,"Service not running");
            return false;
        }
        return true;
    }
}
